package sysUtil


import org.beetl.core.GroupTemplate
import org.beetl.core.Template
import org.beetl.core.resource.StringTemplateResourceLoader

/**
 * Created by Administrator on 2018-12-29.
 */
class BeetlUtil {
    private final static StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
    private final static org.beetl.core.Configuration config = org.beetl.core.Configuration.defaultConfiguration();
    private final static GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, config);

    static getStrTemplate(Map param,String strTemp){
        if(strTemp){
            Template template = groupTemplate.getTemplate(strTemp);
            if(param){
                template.binding(param)
            }
            return  template.render()
        }
        return  null
    }


    static getStrTemplateForEchart(Map param,String strTemp){
        def result
        if(strTemp){
            def temp = strTemp.replaceAll("@and","&&").replaceAll("@逗号",",")
            Template template = groupTemplate.getTemplate(temp);
            try {
                if(param){
                    template.binding(param)
                }
            }catch (Exception e){

            }
            result =  template.render().replaceAll("@左大括号","{").replaceAll("@右大括号","}").replaceAll("@H换行","<br/>")
        }
        return  result
    }

    /**
     * 只用于select-sql模板
     * @param param
     * @param sqlTemp
     * @return
     */
    static getSelectSQLTemplate(Map param,String sqlTemp){
        Template template = groupTemplate.getTemplate(sqlTemp);
        template.binding(param)
        def sqlNew  = template.render()

        def key = param.valueField
        def label = param.labelField
        def keyword =  param?.keyword
        sqlNew = "select ${key} value , ${label} name from (${sqlNew}) ta where 1=1 "

        if( keyword && keyword.indexOf("'") == -1){
            sqlNew += "  and ( ${key} like '%${keyword}%' or ${label} like '%${keyword}%' ) "
        }

        return  String.valueOf(sqlNew)
    }
}
